﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLNH.MyUserControls
{
    public partial class SearchToolbarUC : UserControl
    {
        public delegate void ButtonFindClickHandler(object sender, EventArgs e);
        public event ButtonFindClickHandler ButtonFindClick = null;

        public delegate void ButtonReloadClickHandler(object sender, EventArgs e);
        public event ButtonReloadClickHandler ButtonReloadClick = null;

        public string StringToFind
        {
            get { return this.tb_name.Text; }
        }

        public object GroupIDToFind
        {
            get
            {
                if (this.chb_group.Checked == true)
                    return this.cb_group.SelectedValue;
                else return null;
            }
        }

        public string GroupNameToFind
        {
            get
            {
                if (this.chb_group.Checked == true)
                    return this.cb_group.Text;
                else return "";
            }
        }

        public string LabelString
        {
            get { return this.lb_stringToFind.Text; }
            set { this.lb_stringToFind.Text = value; }
        }

        public string GroupLabel
        {
            get { return this.chb_group.Text; }
            set { this.chb_group.Text = value; }
        }

        public SearchToolbarUC()
        {
            InitializeComponent();
        }

        public void SetGroupComboboxData(object dataSource, string displayMember, string valueMember)
        {
            this.cb_group.DataSource = dataSource;
            this.cb_group.DisplayMember = displayMember;
            this.cb_group.ValueMember = valueMember;
            this.bt_find.Enabled = false;
        }

        private void bt_find_Click(object sender, EventArgs e)
        {
            if (this.ButtonFindClick != null)
            {
                this.ButtonFindClick(sender, e);
                this.bt_reload.Enabled = true;
                this.bt_find.Enabled = false;
            }
        }

        private void bt_reload_Click(object sender, EventArgs e)
        {
            if (this.ButtonReloadClick != null)
            {
                this.tb_name.Focus();
                this.tb_name.SelectAll();
                this.chb_group.Checked = false;
                this.ButtonReloadClick(sender, e);
                this.bt_reload.Enabled = false;
            }
        }

        private void InfoChanged(object sender, EventArgs e)
        {
            int id = Int32.Parse(((Control)sender).Tag.ToString());
            switch (id)
            {
                case 0:
                    if (this.bt_find.Enabled == false && !this.tb_name.Text.Equals(""))
                        this.bt_find.Enabled = true;
                    else if (this.tb_name.Text.Equals(""))
                        this.bt_find.Enabled = false;
                    break;
                case 1:
                    this.cb_group.Enabled = this.chb_group.Checked;
                    break;
                case 2:
                    break;
                default:
                    break;
            }
            if (this.bt_find.Enabled == false)
                this.bt_find.Enabled = true;
        }
    }
}
